package library;

import java.util.ArrayList;
import java.util.List;
 

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
 

import android.content.Context;
import android.util.Log;
 
public class UserFunctions {
     
    private JSONParser jsonParser;
    
    private static String loginAndRegistrationURL = "http://network-sio.fr/randox/loginAndRegistration.php";
    private static String updateURL = "http://network-sio.fr/randox/updateUser.php";
    private static String searchURL = "http://network-sio.fr/randox/search.php";
    private static String infoUserURL = "http://network-sio.fr/randox/infoUser.php";
    private static String checkConversationURL = "http://network-sio.fr/randox/checkConversation.php";
    private static String closeConversationURL = "http://network-sio.fr/randox/closeConversation.php";
    private static String sendAndPrintMessageURL = "http://network-sio.fr/randox/sendAndPrintMessage.php";
     
    private static String login_tag = "login";
    private static String register_tag = "register";
    private static String updateUser_tag = "update";
    private static String search_tag = "search";
    private static String infoUser_tag = "info";
    private static String checkConversation_tag = "checkConversation";
    private static String closeConversation_tag = "closeConversation";
    private static String sendMessage_tag = "sendMessage";
    private static String printMessage_tag = "printMessage";
     
    public UserFunctions(){
        jsonParser = new JSONParser();
    }
     
    /**
     * function make Login Request
     * @param email
     * @param password
     * */
    public JSONObject loginUser(String email, String password){
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", login_tag));
        params.add(new BasicNameValuePair("email", email));
        params.add(new BasicNameValuePair("password", password));
        JSONObject json = jsonParser.getJSONFromUrl(loginAndRegistrationURL, params);
        return json;
    }
    
    public JSONObject updateStatus(Integer status, String email){
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", updateUser_tag));
        params.add(new BasicNameValuePair("status", status.toString()));
        params.add(new BasicNameValuePair("email", email));
        JSONObject json = jsonParser.getJSONFromUrl(updateURL, params);
        return json;
    }
     
    
    public JSONObject searchUser(String email, String sex){
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", search_tag));
        params.add(new BasicNameValuePair("email", email));
        params.add(new BasicNameValuePair("sex", sex));
        JSONObject json = jsonParser.getJSONFromUrl(searchURL, params);
        return json;
    }
    
    
    public JSONObject infosUser(String email){
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", infoUser_tag));
        params.add(new BasicNameValuePair("email", email));
        JSONObject json = jsonParser.getJSONFromUrl(infoUserURL, params);
        return json;
    }
    
    
    public JSONObject checkConversation(String email){
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", checkConversation_tag));
        params.add(new BasicNameValuePair("email", email));
        JSONObject json = jsonParser.getJSONFromUrl(checkConversationURL, params);
        return json;
    }
    
    
    public JSONObject closeConversation(String email){
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", closeConversation_tag));
        params.add(new BasicNameValuePair("email", email));
        JSONObject json = jsonParser.getJSONFromUrl(closeConversationURL, params);
        return json;
    }
    
    
    public JSONObject sendMessage(Integer idUser,Integer idConversation,String message_body){
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", sendMessage_tag));
        params.add(new BasicNameValuePair("idUser", idUser.toString()));
        params.add(new BasicNameValuePair("idConversation", idConversation.toString()));
        params.add(new BasicNameValuePair("message", message_body));
        JSONObject json = jsonParser.getJSONFromUrl(sendAndPrintMessageURL, params);
        return json;
    }
    
    
    public JSONObject printMessage(Integer idConversation){
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", printMessage_tag));
        params.add(new BasicNameValuePair("idConversation", idConversation.toString()));
        JSONObject json = jsonParser.getJSONFromUrl(sendAndPrintMessageURL, params);
        return json;
    }
    
    
    /**
     * function make Login Request
     * @param name
     * @param email
     * @param password
     * */
    public JSONObject registerUser(String firstname, String lastname, String email,String password,String age,String sex ){
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", register_tag));
        params.add(new BasicNameValuePair("firstname", firstname));
        params.add(new BasicNameValuePair("lastname", lastname));
        params.add(new BasicNameValuePair("email", email));
        params.add(new BasicNameValuePair("password", password));
        params.add(new BasicNameValuePair("age", age));
        params.add(new BasicNameValuePair("sex", sex));
        JSONObject json = jsonParser.getJSONFromUrl(loginAndRegistrationURL, params);
        return json;
    }
     
    /**
     * Function get Login status
     * */
    public boolean isUserLoggedIn(Context context){
        DatabaseHandler db = new DatabaseHandler(context);
        int count = db.getRowCount();
        if(count > 0){
            return true;
        }
        return false;
    }
     
    /**
     * Function to logout user
     * Reset Database
     * */
    public boolean logoutUser(Context context){
        DatabaseHandler db = new DatabaseHandler(context);
        db.resetTables();
        return true;
    }
     
}